<?php

namespace app\api\logic;
use app\admin\controller\system\Config;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use app\api\logic\CheckLogic;
use think\facade\Db;

class SendEmailLogic
{


    /**
     * @desc   获取fb广告
     * @author luckyXiong
     * @date   2025-04-24
     */
    public function get_fb_price(){
        // 获取当前日期的前一天
        $yesterday = date('Y-m-d', time() - 86400);
        $where     = [];
        $where[]   = ['status','=','1'];
        $where[]   = ['platform_id','=','1'];
        $title     = 'shecurve';
        $where[]   =  ['info', 'like', '%' . $title . '%'];
        $customer_list= Db::name('advaccount')->field('id,advertiser_id')->where($where)->find();
        $ads_id    =  $customer_list['advertiser_id'];
        $price     = (new CheckLogic())->get_fb_price($ads_id,$yesterday);
        if(isset($price['error']['message']) && $price['error']['message']){
               $message = $price['error']['message'];
               //发送邮件消息通知
               self::send_email($message);
        }
    }




    /**
     * @desc   邮件发送
     * @author luckyXiong
     * @date   2025-04-22
     */
    public function send_email($content){
        $mail = new PHPMailer(true);
        // 服务器设置
        $mail->isSMTP();                                            // 使用SMTP发送
        $mail->Host       = 'smtp.qq.com';                          // 设置SMTP服务器地址
        $mail->SMTPAuth   = true;                                   // 启用SMTP认证
        $mail->Username   = '3268506673';                           // QQ邮箱地址
        $mail->Password   = 'yrqkhtvlexnzdcbg';                     // QQ邮箱SMTP授权码
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // 启用TLS加密
        $mail->Port       = 587;                                    // SMTP端口
        // 收件人设置
        $mail->setFrom('3268506673@qq.com', 'Mailer');
        $emails_number =Config('Logisticskey.email_number');
        foreach($emails_number as $address){
            $mail->addAddress($address, 'Recipient');
        }
        // 内容
        $mail->isHTML(true);                                        // 设置邮件内容为HTML
        $mail->Subject = 'Fb广告花费错误信息';
        $mail->Body    = $content;
        $res = $mail->send();
        return $res;
    }
}